﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;

namespace PriceWatcher
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            listView1.Columns.Add("名称",100);
            listView1.Columns.Add("价格",100);
            listView1.Columns.Add("网址",200);
            listView1.Columns.Add("标识",100);
            XmlNodeList list = Program.g_xmlDoc.GetElementsByTagName("Target");
            foreach (XmlNode node in list)
            {
                Target target = new Target();
                target.Read(node);
                this.listView1.Items.Add(target);
            }
        }

        private void butNewTarget_Click(object sender, EventArgs e)
        {
            frmNewTarget dlg = new frmNewTarget();
            dlg.ShowDialog();
            if (dlg.target != null)
            {
                listView1.Items.Add(dlg.target);
                dlg.target.Write(Program.g_xmlDoc, Program.g_xmlFileName);
            }
            dlg.Dispose();
        }
        
        private void butEditTarget_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            int i=listView1.SelectedIndices[0];
            frmNewTarget dlg = new frmNewTarget();
            dlg.target = listView1.Items[i] as Target;
            dlg.ShowDialog();
            if (dlg.target != null)
            {
                listView1.Items[i] = dlg.target;
                XmlNode t = Program.g_xmlDoc.GetElementsByTagName("Target")[i];
                dlg.target.Edit(t, Program.g_xmlFileName);
            }
            dlg.Dispose();
        }

        private void butDeleteTarget_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count==0) return;
            int i = listView1.SelectedIndices[0];
            Target t = listView1.Items[i] as Target;
            string msg = string.Format("确定要删除当前监控对象{0}么？", t.TargetName);
            DialogResult r = MessageBox.Show(msg, "删除监控对象", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                listView1.Items.RemoveAt(i);
                XmlDocument doc = Program.g_xmlDoc;
                XmlNodeList list= doc.GetElementsByTagName("Target");
                XmlNode root = doc.GetElementsByTagName("AllTargets")[0];
                root.RemoveChild(list[i]);
                doc.Save(Program.g_xmlFileName);
            }
        }

        private void chkWatch_Click(object sender, EventArgs e)
        {
            if (chkWatch.Checked)
            {
                Target t = this.listView1.Items[0] as Target;
                System.Diagnostics.Debug.Write("out of the watch: "+t.TargetPrice);
                t.Watch();
            }
        }
    }
}